import * as categoryApi from '@/api/category.js';
export default {
  namespaced: true,
  state() {
    return {
      categoryList: [],
      // 二级分类列表
      subCategoryGoods: {
        items: [],
        page: 1,
        pageSize: 10,
        counts: 0,
        pages: 1,
      },
      // 商品种类详情
      subCategoryList: {
        brands: [],
        categories: [],
        goods: [],
        saleProperties: [],
      },
    };
  },
  getters: {
    subCategoryFilters(state) {
      return [
        {
          id: 'pinpai',
          name: '品牌',
          properties: state.subCategoryList.brands,
        },
        ...state.subCategoryList.saleProperties,
      ];
    },
  },
  mutations: {
    CATEGORY(state, newValue) {
      state.categoryList = newValue;
    },
    SET_SUB_CATEGORY_GOODS(state, newValue) {
      state.subCategoryGoods = {
        ...newValue,
        items: state.subCategoryGoods.items.concat(newValue.items),
      };
    },
    SUB_CATEGORY_GOODS(state, newValue) {
      state.subCategoryList = newValue;
    },
  },
  actions: {
    async updateCategoryList({ commit }, id) {
      const res = await categoryApi.getCategory(id);
      commit('CATEGORY', res);
    },
    // 分类列表 卡片懒加载
    async updateSubCategoryGoods({ commit }, params) {
      const res = await categoryApi.getSubCategoryGoods(params);
      commit('SET_SUB_CATEGORY_GOODS', res);
    },
    // 二级商品分类  文字详情
    async updateSubCategoryList({ commit }, id) {
      const res = await categoryApi.getSubCategory(id);
      commit('SUB_CATEGORY_GOODS', res);
    },
  },
};
